package com.two.lbms.syt.controller;

import com.two.lbms.base.BaseController;
import com.two.lbms.base.ResultInfo;
import com.two.lbms.dao.BookInfoMapper;
import com.two.lbms.dao.ReaderMapper;
import com.two.lbms.syt.dto.LendDTO;
import com.two.lbms.syt.query.LendQuery;
import com.two.lbms.syt.service.LendService;
import com.two.lbms.vo.BookInfo;
import com.two.lbms.vo.Reader;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("lend")
@Slf4j
public class LendController extends BaseController {

    @Resource
    private LendService lentService;
    @Resource
    private ReaderMapper readerMapper;

    @Resource
    private BookInfoMapper bookInfoMapper;

    /**
     * 进入借书页面
     *
     * @return
     */
    @RequestMapping("lendPage")
    public String toLentPage() {
        return "syt/borrow";

    }

    /**
     * 根据用户id,图书id分页查询用户信息
     * 判断用户id是否存在，通过id查询用户信息
     *
     * @param lentQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> queryAllReaderList(LendQuery lentQuery) {
        if (lentQuery.getReaderName() != null && lentQuery.getReaderName() != "") {
            Reader reader = readerMapper.selectByPrimaryName(lentQuery.getReaderName());
            lentQuery.setReaderId(reader.getId());
        }
        if (lentQuery.getBookName() != null && lentQuery.getBookName() != "") {
            BookInfo bookInfo = bookInfoMapper.selectByPrimaryName(lentQuery.getBookName());
            lentQuery.setBookId(bookInfo.getId());
        }
        return lentService.queryByParamsForTable(lentQuery);
    }

    /**
     * 调用service方法执行借书操作，返回借书信息
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("addLend")
    @ResponseBody
    public ResultInfo addLendMsg(LendDTO lendList) {
        log.info("前端获取到的借阅信息 : {}",lendList);
        //添加借书记录
        lentService.addLentList(lendList);
        //修改馆藏记录
        //lentService.updateCollectionList();
        return success("借书记录添加成功");
    }

    /**
     * 更新借书记录
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("updateLend")
    @ResponseBody
    public ResultInfo updateLendMsg(LendDTO lendDTO) {
        log.info("要修改的借阅信息 : {}", lendDTO);
        lentService.updateLentMsg(lendDTO);
        return success("修改记录成功");

    }

    /**
     * 进入添加/修改页面
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("toAddOrUpdatePage")
    public String toAddOrUpdatePage(Integer lendId, HttpServletRequest request) {

        // 判断lendId是否为空
        if (lendId != null) {
            // 通过ID查询借书数据（书名，读者名）
            LendDTO lendDTO = lentService.queryById(lendId);
            log.info("返回给前端的借阅信息 : {}", lendDTO);
            // LendQuery lendQuery =  lentService.queryById(lendId);
            // 将数据设置到请求域中
            request.setAttribute("lendDTO", lendDTO);
        }

        return "syt/updateandadd/add_update";
    }

    /**
     * 删除借阅记录
     *
     * @param ids
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteLend(Integer[] ids) {
        lentService.deleteLendList(ids);
        return success("借书记录删除成功");
    }

}

